package com.ruoyi.utils;

import com.ruoyi.domain.HhOrderDetailVO;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 打印展示处理
 * @author chenjc
 * @since 2020/12/18 11:15
 */
public class PrintPageUtil {

    public final static Integer pageSize = 18;

    public static List<Map> printPage(List<HhOrderDetailVO> odList) {

        List<Map> pageResult = new ArrayList<>();
        Map<String, Object> pageMap = null;

        int totalSize = odList.size();
        //计算总页数
        int totalPage = totalSize % pageSize == 0 ? totalSize / pageSize : totalSize / pageSize + 1;

        List<HhOrderDetailVO> dataList = null;
        BigDecimal pageAmount = null;
        StringBuffer productName = null;
        StringBuffer productNum = null;

        for (int i = 0; i < totalPage; i++) {
            dataList = new ArrayList();
            //每页展示条数
            dataList.addAll(odList.subList(i * pageSize, i == totalPage - 1 ? totalSize : (i + 1) * pageSize));
            pageAmount = new BigDecimal("0");
            for (HhOrderDetailVO hhOrderDetailVO : dataList) {
                //拼接商品名：品牌+商品
                productName = new StringBuffer();
                if (hhOrderDetailVO.getProductBrand() != null) {
                    productName.append(hhOrderDetailVO.getProductBrand()).append(" ");
                }
                productName.append(hhOrderDetailVO.getProductName());
                hhOrderDetailVO.setProductName(productName.toString());
                //拼接数量：数量+单位
                productNum = new StringBuffer();
                productNum.append(hhOrderDetailVO.getProductNum());
                if (StringUtils.isNotBlank(hhOrderDetailVO.getProductUnit())) {
                    productNum.append(hhOrderDetailVO.getProductUnit());
                }
                hhOrderDetailVO.setProductNum(productNum.toString());
                //小计本页金额
                pageAmount = pageAmount.add(new BigDecimal(hhOrderDetailVO.getDetailAmount()));
            }
            //补足本页
//            while (dataList.size() < pageSize) {
//                dataList.add(new HhOrderDetailVO());
//            }
            pageMap = new HashMap<>();
            pageMap.put("totalPage", String.valueOf(totalPage));
            pageMap.put("currentPage", String.valueOf(i + 1));
            pageMap.put("pageAmount", "￥" + pageAmount.toString());
            pageMap.put("pageAmountCN", Number2CNUtil.number2CN(pageAmount));
            pageMap.put("dataList", dataList);
            pageResult.add(pageMap);
        }

        return pageResult;
    }

}
